//
// File:	GuiManager.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	28/01/2010 at 18:36:41
// Purpose:	
//
#include <QtGui/QHBoxLayout>
#include "Base.hpp"
#include "GuiManager.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
GuiManager::GuiManager()
	:	m_pPanelStack(NULL),
		m_panelMap()
{
}

GuiManager::~GuiManager()
{
}

//------------------------------------------------------------------
// Init
//------------------------------------------------------------------
bool GuiManager::Init(bool bShow)
{
	QWidget *pWidgetWindow = dynamic_cast<QWidget *>(GlobalVariables::DisplayManager::Get().GetWindow());
	if (pWidgetWindow == NULL)
	{
		Trace::warning << "GuiManager::Init: the window does not inherit QWidget." << std::endl;
		return false;
	}
	m_pPanelStack = new QStackedWidget(pWidgetWindow);
	if (pWidgetWindow->layout() != NULL)
		pWidgetWindow->layout()->addWidget(m_pPanelStack);
	SetVisible(bShow);
	return true;
}

//------------------------------------------------------------------
// Add/Remove/Find/Show Panel
//------------------------------------------------------------------
bool GuiManager::AddPanel(const std::string &panelName, IGuiPanel *pPanel)
{
	if (FindPanel(panelName) != NULL)
	{
		Trace::warning << "GuiManager::AddPanel: " << panelName << " already exists." << std::endl;
		return false;
	}
	QWidget *pWidgetPanel = dynamic_cast<QWidget *>(pPanel);
	if (pWidgetPanel == NULL)
	{
		Trace::warning << "GuiManager::AddPanel: " << panelName << " does not inherit QWidget." << std::endl;
		return false;
	}
	m_panelMap[panelName] = pPanel;
	pWidgetPanel->setParent(m_pPanelStack);
	m_pPanelStack->addWidget(pWidgetPanel);
	return true;
}

IGuiPanel *GuiManager::RemovePanel(const std::string &panelName)
{
	IGuiPanel *pPanel = FindPanel(panelName);
	QWidget *pWidgetPanel = dynamic_cast<QWidget *>(pPanel);
	if (pWidgetPanel != NULL)
	{
		m_pPanelStack->removeWidget(pWidgetPanel);
	}
	if (pPanel != NULL)
	{
		m_panelMap.erase(panelName);
		return pPanel;
	}
	return NULL;
}

IGuiPanel *GuiManager::FindPanel(const std::string &panelName)
{
	std::map<std::string, IGuiPanel *>::iterator iter = m_panelMap.find(panelName);
	if (iter != m_panelMap.end())
	{
		return iter->second;
	}
	return NULL;
}

bool GuiManager::ShowPanel(const std::string &panelName)
{
	IGuiPanel *pPanel = FindPanel(panelName);
	QWidget *pWidgetPanel = dynamic_cast<QWidget *>(pPanel);
	if (pWidgetPanel != NULL)
	{
		m_pPanelStack->setCurrentWidget(pWidgetPanel);
		return true;
	}
	return false;
}

//------------------------------------------------------------------
// Visibility
//------------------------------------------------------------------
void GuiManager::SetVisible(bool bVisible)
{
	if (m_pPanelStack != NULL)
		m_pPanelStack->setVisible(bVisible);
}

bool GuiManager::IsVisible() const
{
	if (m_pPanelStack == NULL)
		return false;
	return m_pPanelStack->isVisible();
}

//------------------------------------------------------------------
// Current Panel
//------------------------------------------------------------------
IGuiPanel *GuiManager::GetCurrentPanel() const
{
	return dynamic_cast<IGuiPanel *>(m_pPanelStack->currentWidget());
}
